﻿using Microsoft.AspNetCore.Authentication.OAuth.Claims;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System.Diagnostics;

namespace Blog.Core.Api.Filter
{
    public class ActionFilter : IAsyncActionFilter
    {
        //依赖注入
        private readonly ILogger<ActionFilter> _logger;
        //生成构造函数
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            
            Stopwatch sw = new Stopwatch();//先实例 Stopwatch这个类是一个秒表 是专门统计时间的
            sw.Start();//开始计时
            await next();//调用action方法
            sw.Stop();//停止计时
            long n = sw.ElapsedMilliseconds;//所用的毫秒
            //var log = NLog.LogManager.GetCurrentClassLogger();
            //log.Info("运行" + n.ToString() + "毫秒");
            _logger.LogInformation("运行了：" + Convert.ToString(n) + "毫秒");//记录NLog日志 
        }

    }
}
